package com.flynn.rock.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {

    private Integer pageNum;

    private Integer pageSize;

    private Integer totalPage;

    private Long totalSize;

    private List<T> content;

    public <R> PageResult<R> map(Function<T, R> function) {
        List<R> content = Optional.ofNullable(this.content)
                .orElse(Collections.emptyList()).stream()
                .map(function).toList();
        return PageResult.<R>builder()
                .pageNum(this.pageNum)
                .pageSize(this.pageSize)
                .totalPage(this.totalPage)
                .totalSize(this.totalSize)
                .content(content)
                .build();
    }
}
